package com.moonciki.artadmin.core.utils.excel;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.InputStream;

/** 新版本 **/
class XExcelReader extends ExcelReader{

    protected XExcelReader(InputStream input, int columnLength, int sheetNumber) throws Exception{
        this.input = input;
        this.columnLength = columnLength;
        this.sheetNumber = sheetNumber;

        workbook = new XSSFWorkbook(input);
        currentSheet = workbook.getSheetAt(sheetNumber);

        lastRowNum = currentSheet.getLastRowNum();

        formulaEvaluator =  workbook.getCreationHelper().createFormulaEvaluator();
    }

    @Override
    public Object[] readExcel(int rowNumber) {
        XSSFRow xssfRow = (XSSFRow)currentSheet.getRow(rowNumber);

        boolean isEmpty = true;

        Object[] valueArray = new Object[columnLength];

        if (xssfRow != null) {

            for(int i=0; i<columnLength; i++){

                //String colName = columnArray[i];

                XSSFCell cell = xssfRow.getCell(i);

                Object cellValue = null;
                if(cell != null){
                    cellValue = getValue(cell,i);
                }

                valueArray[i] = cellValue;

                if(cellValue != null && StringUtils.isNotBlank(cellValue.toString())){
                    isEmpty = false;
                }

            }
        }

        //如果这一行没有任何数据
        if(isEmpty){
            valueArray = null;
        }

        return valueArray;
    }

}
